<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<!-- 导航栏 -->
<div class="nav">
    <!-- logo -->
    <img src="img/cat.jpg" alt="">
    <!-- 标题 -->
    <span class="title">Java 在线OJ 系统</span>
    <!-- 空白的占位符 -->
    <span class="spacer"></span>
    <!-- 右侧的几个链接 -->
    <a class="navbar-brand font-weight-bold" href="index.html">主页</a>
    <a class="navbar-brand font-weight-bold" href="javascript:void(0);"  onclick=logout()>注销</a>
</div>

<!-- 登录页面的页面容器, 为了和其他页面样式区分开, 使用不同的类名 -->
<div class="login-container" >
    <div class="login-dialog" style="height: 390px">
        <!-- 标题 -->
        <h3>忘记密码：邮箱验证</h3>

        <div class="row">
            <span>邮箱</span>
            <input type="text" id="email" name="email" placeholder="用户名绑定的邮箱地址" style="font-size: 15px">
        </div>

        <div class="row">
            <span>验证码</span>
            <input type="text" id="emailcode" name="emailcode">
        </div>

        <div class="row submit-row">
            <button id="sendCode" onclick=sendCode() >发送邮箱验证码</button>
        </div>

        <div class="row submit-row">
            <button id="submit"  onclick=aaa() >确认</button>
        </div>



    </div>
</div>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>

    //1、获取到注册的控件信息
    let Lemail = document.querySelector("#email");
    let LemailCode =document.querySelector("#emailcode");


    function sendCode() { // 发送邮件的AJAX请求

        if(Lemail.value===""){
            alert("邮箱不能为空,请重新输入!");
            return;
        }

        // 2、发送ajax请求，发送邮件
        $.ajax({
            url: "state/emailSend?To="+Lemail.value,
            method:'GET',
            success:function (data,status) {

                alert(data.message);
            },error:function (data) {
                 alert("服务器异常,发送邮件失败!")
            }
        })

        f();

        function f() {
            //将按钮禁用
            let btn = document.querySelector("#sendCode");
            btn.disabled = true;
            btn.style.cursor = "not-allowed"; //鼠标显示禁用图标

            //按钮显示60秒倒计时
            let second = 120;
            let counter = setInterval(function () {
                second--;
                if (second > 0) {
                    btn.innerHTML = second + "秒后重新获取";
                }
            }, 1000);

            //60秒时间间隔到了之后清除倒计时，按钮恢复点击功能
            setTimeout(function () {
                //清除倒计时定时器
                clearInterval(counter);
                //按钮恢复
                btn.innerHTML = "获取验证码";
                btn.disabled = false;
                btn.style.cursor = "";
            }, 30000);
        }

    }


    function aaa() {

        if(Lemail.value==="" || LemailCode.value===""){
            alert("邮箱或验证码不能为空,请重新输入!");
            return;
        }

        let body = {
            email:Lemail.value,
            code:LemailCode.value
        }

        $.ajax({
            url:"state/emailRequire",
            type: "post",
            data:JSON.stringify(body),
            contentType:"application/json;charset=utf8",
            success:function (data) {
                 alert(data.message);
                 if(data.state===5008){
                     location.href="updatePassword.html?email="+Lemail.value;
                 }
            } ,error:function () {
                alert("服务器错误,验证失败");
            }
        });

    }

    function logout() {
        $.ajax({
            url:"state/logout",
            type:"POST",
            success:function (data,status) {
                alert(data.message);
                if(data.state===5002){
                    location.href="login.html";
                }
            }
        })
    }



</script>


</body>
</html>